import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, OPPO_AD_CONFIG } from '../../frame/constants';
import { UI_Listen, UI_Layer } from '../../config/ui_def'
cc.Class({
    extends: cc.Component,

    properties: {
        item_icon: cc.Sprite,
        item_title: cc.Label,
        item_detail: cc.Label,
        item_num: cc.Label,
        adGet_btn: cc.Node,
        tip_lable: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {

    },

    updatePage(param) {
        this.info = param.info
        this.item_title.string = `${this.info.name}`
        this.item_detail.string = `${this.info.description}`
        this.item_num.string = ` x${this.info.num}`
        switch (common.sdk_env) {
            case Sdk_Platform.OPPO:
                if (param.double) {
                    this.adGet_btn.active = true
                    this.tip_lable.active = false
                }
                else {
                    this.adGet_btn.active = false
                    this.tip_lable.active = true
                }
                break
        }
        if (this.info.type == 1) {
            this.item_icon.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.id}`)
        } else if (this.info.type == 3) {
            this.item_icon.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.icon}`)
        }
        //30%弹插页
        this.adSwitch(true)
        let num = Math.ceil(Math.random() * 10)
        if (num > 3) {
            // console.log(num)
            //啥也不做
        } else {
            // vivo弹插页
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    break
                case Sdk_Platform.VIVO:
                    gg.eventManager.emit('main', { type: UI_Listen.MAIN_SHOWVIDEO }, () => {
                        gg.uiManager.addPage(UI_Layer.Msg, {
                            name: 'chayeAdFake',
                            isShowAd: true,
                            isShowBanner: false,
                            pageName: 'storePage'
                        })
                    })
                    break
                case Sdk_Platform.OPPO:
                    
                    break
                default:
                    break
            }
        }
    },
    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    let guideList = he.local.getJson('guideList', 'user')
                    if (!guideList[6]) {
                        return
                    }
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 11,
                        position: cc.Vec2(0, -470)
                    })
                    break
            }
        } else {
            //  关掉广告
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    break
            }
        }
    },
    adGetSever(){
        this.uiBase.closePage()
        switch (common.sdk_env) {
            case Sdk_Platform.OPPO:
                he.ad.showVideo(OPPO_AD_CONFIG.videoId[8], () => {
                    if (this.info.type == 1) {
                        let bagList = he.local.getJson('bag', 'user')
                        bagList.push(
                            { id: this.info.id, num: this.info.num },
                        )
                        he.local.setJson('bag', bagList, 'user')

                    } else if (this.info.type == 3) {
                        let cardList = he.local.getJson('card', 'user')
                        cardList.push({ id: this.info.icon, lv: 1 })
                        he.local.setJson('card', cardList, 'user')
                    }
                    this.uiBase.closePage()
                    gg.uiManager.addPage(UI_Layer.Click, {
                        name: 'getStoreItemPage',
                        cover: true,
                        info: this.info,
                        double : false,
                    })
                   
                })
                break
            case Sdk_Platform.VIVO:
                break
            default:
                let cardList = he.local.getJson('card', 'user')
                cardList.push({ id: this.cardId, lv: 1 })
                he.local.setJson('card', cardList, 'user')
                gg.eventManager.emit('card', { type: UI_Listen.SKILL_ADDCARD })
                this.uiBase.closePage()
                gg.uiManager.addPage(UI_Layer.Click, {
                    name: 'getStoreItemPage',
                    cover: true,
                    info: this.info,
                    double : false,
                })
               
                break
        }
        if (guideList[6]) {
           
        }
    },
    closeUI() {
        this.adSwitch(false)
    },

    closeServer() {
        this.uiBase.closePage()
    },
});
